﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using NUnit.Framework;
using DBSourceToolsLib;
using DBSourceToolsLib.SysObjects;
using System.Data.SqlClient;
//using Microsoft.SqlServer.Management.Smo; // 2005
using Microsoft.SqlServer.Management.Sdk.Sfc; // 2008

namespace TestDBSourceToolsLibNUnit
{
    [TestFixture]
    [Ignore]
    public class TestUrnParser
    {
        [Test]
        public void TestGetStoredProcFromUrn()
        {
            Urn oUrn = new Urn("Server[@Name='AEM129']/Database[@Name='AdventureWorks']/StoredProcedure[@Name='uspUpdateEmployeeHireInfo' and @Schema='HumanResources']");
            DBSourceToolsLib.SysObjects.UrnParser oUrnParser = new UrnParser(oUrn);
            string strType = oUrnParser.getTypeName();
            Assert.IsTrue(strType.CompareTo( SysObjectType.Proc.ToString()) == 0, "UrnParser did not return a storedProc");
            string strSysObjectName = oUrnParser.getObjectName();
            Assert.IsTrue(strSysObjectName.CompareTo("HumanResources.uspUpdateEmployeeHireInfo") == 0, "UrnParser did not return HumanResources.uspUpdateEmployeeHireInfo");
        }
        [Test]
        public void TestGetTableFromUrn()
        {
            Urn oUrn = new Urn("Server[@Name='AEM129']/Database[@Name='AdventureWorks']/Table[@Name='Department' and @Schema='HumanResources']");
            DBSourceToolsLib.SysObjects.UrnParser oUrnParser = new UrnParser(oUrn);
            string strType = oUrnParser.getTypeName();
            Assert.IsTrue(strType.CompareTo(SysObjectType.Table.ToString()) == 0, "UrnParser did not return a Table");
            string strSysObjectName = oUrnParser.getObjectName();
            Assert.IsTrue(strSysObjectName.CompareTo("HumanResources.Department") == 0, "UrnParser did not return HumanResources.Department");
        }
        [Test]
        public void TestGetTableFromUrnNoSchema()
        {
            Urn oUrn = new Urn("Server[@Name='AEM129']/Database[@Name='AdventureWorks']/Table[@Name='ErrorLog' and @Schema='dbo']");
            DBSourceToolsLib.SysObjects.UrnParser oUrnParser = new UrnParser(oUrn);
            string strType = oUrnParser.getTypeName();
            Assert.IsTrue(strType.CompareTo(SysObjectType.Table.ToString()) == 0, "UrnParser did not return a Table");
            string strSysObjectName = oUrnParser.getObjectName();
            Assert.IsTrue(strSysObjectName.CompareTo("ErrorLog") == 0, "UrnParser did not return ErrorLog");
        }

        [Test]
        public void TestGetFunctionFromUrn()
        {
            Urn oUrn = new Urn("Server[@Name='AEM129']/Database[@Name='AdventureWorks']/UserDefinedFunction[@Name='ufnGetAccountingEndDate' and @Schema='dbo']");
            DBSourceToolsLib.SysObjects.UrnParser oUrnParser = new UrnParser(oUrn);
            string strType = oUrnParser.getTypeName();
            Assert.IsTrue(strType.CompareTo(SysObjectType.Function.ToString()) == 0, "UrnParser did not return a Function");
            string strSysObjectName = oUrnParser.getObjectName();
            Assert.IsTrue(strSysObjectName.CompareTo("ufnGetAccountingEndDate") == 0, "UrnParser did not return ufnGetAccountingEndDate");
        }

        [Test]
        public void TestGetDataTypeFromUrn()
        {
            Urn oUrn = new Urn("Server[@Name='AEM129']/Database[@Name='AdventureWorks']/UserDefinedDataType[@Name='Flag' and @Schema='dbo']");
            DBSourceToolsLib.SysObjects.UrnParser oUrnParser = new UrnParser(oUrn);
            string strType = oUrnParser.getTypeName();
            Assert.IsTrue(strType.CompareTo(SysObjectType.DataType.ToString()) == 0, "UrnParser did not return a DataType");
            string strSysObjectName = oUrnParser.getObjectName();
            Assert.IsTrue(strSysObjectName.CompareTo("Flag") == 0, "UrnParser did not return Flag");
        }
        [Test]
        public void TestGetViewFromUrn()
        {
            Urn oUrn = new Urn("Server[@Name='AEM129']/Database[@Name='AdventureWorks']/View[@Name='vEmployee' and @Schema='HumanResources']");
            DBSourceToolsLib.SysObjects.UrnParser oUrnParser = new UrnParser(oUrn);
            string strType = oUrnParser.getTypeName();
            Assert.IsTrue(strType.CompareTo(SysObjectType.View.ToString()) == 0, "UrnParser did not return a View");
            string strSysObjectName = oUrnParser.getObjectName();
            Assert.IsTrue(strSysObjectName.CompareTo("HumanResources.vEmployee") == 0, "UrnParser did not return HumanResources.vEmployee");
        }
        [Test]
        public void TestGetXmlSchemaFromUrn()
        {
            Urn oUrn = new Urn("Server[@Name='AEM129']/Database[@Name='AdventureWorks']/XmlSchemaCollection[@Name='AdditionalContactInfoSchemaCollection' and @Schema='Person']");
            DBSourceToolsLib.SysObjects.UrnParser oUrnParser = new UrnParser(oUrn);
            string strType = oUrnParser.getTypeName();
            Assert.IsTrue(strType.CompareTo(SysObjectType.XmlSchema.ToString()) == 0, "UrnParser did not return an XmlSchema");
            string strSysObjectName = oUrnParser.getObjectName();
            Assert.IsTrue(strSysObjectName.CompareTo("Person.AdditionalContactInfoSchemaCollection") == 0, "UrnParser did not return Person.AdditionalContactInfoSchemaCollection");
        }

        
        

    }
}
